package org.ala.tcp_feign_common.config;

import java.io.Serializable;

import org.ala.tcp_feign_common.enums.TcpFeignSerializableTypeEnums;
import org.springframework.core.env.Environment;


/**
 * tcp-feign 相关配置
 * 
 * @author 骆毅(ala)
 * @date 2024年4月18日
 */
public class TcpFeignConfigure implements Serializable {
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * 配置文件前缀
	 */
	public static final String PREFIX = "tcp-feign";

	
	/**
	 * 服务端端口号
	 */
	protected Integer port;
	/**
	 * boss线程数
	 * <p>	不配默认cpu核数
	 */
	protected int bossCount = Runtime.getRuntime().availableProcessors();
	/**
	 * work线程数
	 * <p>	不配默认bossCount * 4
	 */
	protected int worderCount = bossCount * 4;
	/**
	 * 序列化/反序列化方式
	 */
	protected TcpFeignSerializableTypeEnums serializableType;
	/**
	 * 心跳间隔
	 * <p>	单位：毫秒（默认10s）
	 */
	protected long heartInterval = 10000l;
	/**
	 * 连续几次心跳失败判定为连接无效
	 * <p>	默认6次
	 */
	protected int allowNoHeartTimes = 6;
	/**
	 * 服务端可接受的最大连接数
	 */
	protected int maxConnecton = 1024;
	/**
	 * 登录验证信息
	 */
	protected String username;
	/**
	 * 
	 */
	protected String password;
	/**
	 * 公共业务线程池coresize
	 */
	protected int commonBizThreadPoolCoreSize = 16;
	/**
	 * 公共业务线程池maxsize
	 */
	protected int commonBizThreadPoolMaxSize = 256;
	/**
	 * 公共业务线程池队列
	 */
	protected int commonBizThreadPoolQueueSize = 1024;
	/**
	 * biz->resp 超时时间
	 * <p>	单位：毫秒
	 * <p>	默认5s
	 */
	protected long bizTimeout = 5000l;
	/**
	 * client resp线程池配置 coresize
	 * <p> 仅用于收到回执时：
	 * <p>		1 反序列化
	 * <p>		2 从每个client持有的FutureMap中找到对应的Future
	 * <p>	默认cpu核数 * 2
	 */
	protected int clientRespBizThreadPoolCoreSize = Runtime.getRuntime().availableProcessors() * 2;
	/**
	 * client resp线程池配置 maxsize
	 */
	protected int clientRespBizThreadPoolMaxSize = clientRespBizThreadPoolCoreSize * 2;
	/**
	 * client resp线程池 queuesize
	 */
	protected int clientRespBizThreadPoolQueueSize = 1024;
	
	
	
	/**
	 * 通过配置文件构建
	 */
	public static TcpFeignConfigure build(Environment env) {
		TcpFeignConfigure conf = new TcpFeignConfigure();
		
		conf.setPort(Integer.parseInt(env.getProperty(PREFIX + ".port")));
		
		try { conf.setBossCount(Integer.parseInt(env.getProperty(PREFIX + ".bossCount"))); }
		catch (Exception e) {}
		
		try { conf.setWorderCount(Integer.parseInt(env.getProperty(PREFIX + ".worderCount"))); }
		catch (Exception e) {}
		
		try { conf.setSerializableType(TcpFeignSerializableTypeEnums.code(env.getProperty(PREFIX + ".serializableType"))); }
		catch (Exception e) {conf.setSerializableType(TcpFeignSerializableTypeEnums.KRYO);}
		
		try { conf.setHeartInterval(Long.parseLong(env.getProperty(PREFIX + ".heartInterval"))); }
		catch (Exception e) {conf.setHeartInterval(10000l);}
		
		try { conf.setAllowNoHeartTimes(Integer.parseInt(env.getProperty(PREFIX + ".allowNoHeartTimes"))); }
		catch (Exception e) {conf.setAllowNoHeartTimes(6);}
		
		conf.setUsername(env.getProperty(PREFIX + ".username"));
		conf.setPassword(env.getProperty(PREFIX + ".password"));
		
		try { conf.setCommonBizThreadPoolCoreSize(Integer.parseInt(env.getProperty(PREFIX + ".commonBizThreadPoolCoreSize"))); }
		catch (Exception e) {conf.setCommonBizThreadPoolCoreSize(16);}
		
		try { conf.setCommonBizThreadPoolMaxSize(Integer.parseInt(env.getProperty(PREFIX + ".commonBizThreadPoolMaxSize"))); }
		catch (Exception e) {conf.setCommonBizThreadPoolMaxSize(256);}
		
		try { conf.setCommonBizThreadPoolQueueSize(Integer.parseInt(env.getProperty(PREFIX + ".commonBizThreadPoolQueueSize"))); }
		catch (Exception e) {conf.setCommonBizThreadPoolQueueSize(1024);}
		
		try { conf.setBizTimeout(Integer.parseInt(env.getProperty(PREFIX + ".bizTimeout"))); }
		catch (Exception e) {conf.setBizTimeout(5000l);}
		
		try { conf.setClientRespBizThreadPoolCoreSize(Integer.parseInt(env.getProperty(PREFIX + ".clientRespBizThreadPoolCoreSize"))); }
		catch (Exception e) {conf.setClientRespBizThreadPoolCoreSize(Runtime.getRuntime().availableProcessors() * 2);}
		
		try { conf.setClientRespBizThreadPoolMaxSize(Integer.parseInt(env.getProperty(PREFIX + ".clientRespBizThreadPoolMaxSize"))); }
		catch (Exception e) {conf.setClientRespBizThreadPoolMaxSize(conf.getClientRespBizThreadPoolCoreSize() * 2);}
		
		try { conf.setClientRespBizThreadPoolQueueSize(Integer.parseInt(env.getProperty(PREFIX + ".clientRespBizThreadPoolQueueSize"))); }
		catch (Exception e) {conf.setClientRespBizThreadPoolQueueSize(1024);}
		
		return conf;
	}
	
	
	public Integer getPort() {
		return port;
	}
	public void setPort(Integer port) {
		this.port = port;
	}
	public int getBossCount() {
		return bossCount;
	}
	public void setBossCount(int bossCount) {
		this.bossCount = bossCount;
	}
	public int getWorderCount() {
		return worderCount;
	}
	public void setWorderCount(int worderCount) {
		this.worderCount = worderCount;
	}
	public TcpFeignSerializableTypeEnums getSerializableType() {
		return serializableType;
	}
	public void setSerializableType(TcpFeignSerializableTypeEnums serializableType) {
		this.serializableType = serializableType;
	}
	public long getHeartInterval() {
		return heartInterval;
	}
	public void setHeartInterval(long heartInterval) {
		this.heartInterval = heartInterval;
	}
	public int getAllowNoHeartTimes() {
		return allowNoHeartTimes;
	}
	public void setAllowNoHeartTimes(int allowNoHeartTimes) {
		this.allowNoHeartTimes = allowNoHeartTimes;
	}
	public int getMaxConnecton() {
		return maxConnecton;
	}
	public void setMaxConnecton(int maxConnecton) {
		this.maxConnecton = maxConnecton;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}


	public int getCommonBizThreadPoolCoreSize() {
		return commonBizThreadPoolCoreSize;
	}


	public void setCommonBizThreadPoolCoreSize(int commonBizThreadPoolCoreSize) {
		this.commonBizThreadPoolCoreSize = commonBizThreadPoolCoreSize;
	}


	public int getCommonBizThreadPoolMaxSize() {
		return commonBizThreadPoolMaxSize;
	}


	public void setCommonBizThreadPoolMaxSize(int commonBizThreadPoolMaxSize) {
		this.commonBizThreadPoolMaxSize = commonBizThreadPoolMaxSize;
	}


	public int getCommonBizThreadPoolQueueSize() {
		return commonBizThreadPoolQueueSize;
	}


	public void setCommonBizThreadPoolQueueSize(int commonBizThreadPoolQueueSize) {
		this.commonBizThreadPoolQueueSize = commonBizThreadPoolQueueSize;
	}


	public long getBizTimeout() {
		return bizTimeout;
	}


	public void setBizTimeout(long bizTimeout) {
		this.bizTimeout = bizTimeout;
	}


	public int getClientRespBizThreadPoolCoreSize() {
		return clientRespBizThreadPoolCoreSize;
	}


	public void setClientRespBizThreadPoolCoreSize(int clientRespBizThreadPoolCoreSize) {
		this.clientRespBizThreadPoolCoreSize = clientRespBizThreadPoolCoreSize;
	}


	public int getClientRespBizThreadPoolMaxSize() {
		return clientRespBizThreadPoolMaxSize;
	}


	public void setClientRespBizThreadPoolMaxSize(int clientRespBizThreadPoolMaxSize) {
		this.clientRespBizThreadPoolMaxSize = clientRespBizThreadPoolMaxSize;
	}


	public int getClientRespBizThreadPoolQueueSize() {
		return clientRespBizThreadPoolQueueSize;
	}


	public void setClientRespBizThreadPoolQueueSize(int clientRespBizThreadPoolQueueSize) {
		this.clientRespBizThreadPoolQueueSize = clientRespBizThreadPoolQueueSize;
	}
	
}
